package redbencity.com.richtext.richeditor;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;


public class ColorPickerView extends View {

    private Paint paint;
    private Paint circlePaint;
    private int[] mark = new int[]{0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000};
    private Bitmap bgBitmap;
    private Canvas canvas;
    private RectF rect;
    private int color = 0xffffffff;
    private int selectX = 50;
    private int selectY = 50;
    private boolean showCircle = false;

    public ColorPickerView(Context context) {
        super(context);
        init();
    }

    public ColorPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ColorPickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        Shader shader = new LinearGradient(0, 0, 0xff, 0, mark, null, Shader.TileMode.CLAMP);
        paint.setShader(shader);
        bgBitmap = Bitmap.createBitmap(0xff, 80, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bgBitmap);
        rect = new RectF();
        circlePaint = new Paint();
        circlePaint.setColor(0xffffffff);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeWidth(2);
        circlePaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        rect.bottom = MeasureSpec.getSize(heightMeasureSpec);
        rect.right = MeasureSpec.getSize(widthMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.canvas.drawRect(0, 0, bgBitmap.getWidth(), bgBitmap.getHeight(), paint);
        canvas.drawBitmap(bgBitmap, null, rect, null);
        if (showCircle) {
            canvas.drawCircle(selectX, selectY, 5, circlePaint);
        }
    }

    public int getColor() {
        return color;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                this.color = bgBitmap.getPixel((int) (event.getX() * 1.0 / getWidth() * bgBitmap.getWidth()),
                        (int) (event.getY() * 1.0 / getHeight() * bgBitmap.getHeight()));
                selectX = (int) (event.getX());
                selectY = (int) (event.getY());
                Log.d("ColorPickerView ", "color " + String.valueOf(Integer.toHexString(this.color)));
                invalidate();
                onSelectListener.onSelectListener(color);
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return super.onTouchEvent(event);
    }

    void setShowCircle(boolean showCircle) {
        this.showCircle = showCircle;
        invalidate();
    }

    private OnSelectListener onSelectListener = new OnSelectListener() {
        @Override
        public void onSelectListener(int color) {

        }
    };

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    interface OnSelectListener {
        void onSelectListener(int color);
    }
}
